package com.sdk.pay.transfer.provider;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.sdk.pay.transfer.MainActivity;
import com.sdk.pay.transfer.constant.PayContext;
import com.sdk.pay.transfer.model.WECHATBean;
import com.sdk.pay.transfer.utils.ExceptionUitl;
import com.sdk.pay.transfer.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import static com.sdk.pay.transfer.provider.Provider.WECHATColumns.codeUrl;
import static com.sdk.pay.transfer.provider.Provider.WECHATColumns.money;
import static com.sdk.pay.transfer.provider.Provider.WECHATColumns.reason;
import static com.sdk.pay.transfer.provider.Provider.WECHATColumns.treated;

public class ProviderHelper {

    public static synchronized WECHATBean get_OneBean_Without_CodeUrl(Context context) {
        Cursor c = null;
        try {
            c = context.getContentResolver()
                    .query(Provider.WECHATColumns.CONTENT_URI,
                            new String[]{Provider.WECHATColumns._ID,
                                    Provider.WECHATColumns.codeUrl,
                                    Provider.WECHATColumns.money,
                                    Provider.WECHATColumns.reason,
                                    Provider.WECHATColumns.treated},
                            Provider.WECHATColumns.treated + "=? and "
                                    + Provider.WECHATColumns.money + "!=? and "
                                    + Provider.WECHATColumns.codeUrl + "=?",
                            new String[]{"0" + "", "0" + "", ""}, null);

            while (c != null && c.moveToNext()) {
                WECHATBean bean = new WECHATBean();
                bean.id = c.getInt(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns._ID));
                bean.codeUrl = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.codeUrl));
                bean.money = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.money));
                bean.reason = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.reason));
//                MainActivity.instance.log("get_OneBean_Without_CodeUrl: " + bean.toString());
                return bean;
            }

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            ExceptionUitl.getInstance().saveExceptionFile("ProviderHelper IllegalArgumentException = "
                    + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    public static synchronized List<WECHATBean> get_ListBean_Without_CodeUrl(Context context) {
        List<WECHATBean> arrayList = new ArrayList<>();
        Cursor c = null;
        try {
            c = context.getContentResolver()
                    .query(Provider.WECHATColumns.CONTENT_URI,
                            new String[]{Provider.WECHATColumns._ID,
                                    Provider.WECHATColumns.codeUrl,
                                    Provider.WECHATColumns.money,
                                    Provider.WECHATColumns.reason,
                                    Provider.WECHATColumns.treated},
                            Provider.WECHATColumns.treated + "=? and "
                                    + Provider.WECHATColumns.money + "!=? and "
                                    + Provider.WECHATColumns.codeUrl + "=?",
                            new String[]{"0" + "", "0" + "", ""}, null);
            
            while (c != null && c.moveToNext()) {
                WECHATBean bean = new WECHATBean();
                bean.id = c.getInt(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns._ID));
                bean.codeUrl = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.codeUrl));
                bean.money = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.money));
                bean.reason = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.reason));
                arrayList.add(bean);
                return arrayList;
            }

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            ExceptionUitl.getInstance().saveExceptionFile("ProviderHelper IllegalArgumentException = "
                    + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    public static synchronized WECHATBean get_OneBean_Without_Report(Context context) {
        Cursor c = null;
        try {
            c = context.getContentResolver()
                    .query(Provider.WECHATColumns.CONTENT_URI,
                            new String[]{Provider.WECHATColumns._ID,
                                    Provider.WECHATColumns.codeUrl,
                                    Provider.WECHATColumns.money,
                                    Provider.WECHATColumns.reason,
                                    Provider.WECHATColumns.treated},
                            Provider.WECHATColumns.treated + "=? and "
                                    + Provider.WECHATColumns.codeUrl + "!=?",
                            new String[]{"1" + "", "" + ""}, null);

            while (c != null && c.moveToNext()) {
                WECHATBean bean = new WECHATBean();
                bean.id = c.getInt(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns._ID));
                bean.codeUrl = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.codeUrl));
                bean.money = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.money));
                bean.reason = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.reason));
                bean.treated = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.treated));
                MainActivity.instance.log("get_OneBean_Without_Report : " + bean.toString());
                return bean;
            }

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            ExceptionUitl.getInstance().saveExceptionFile("ProviderHelper IllegalArgumentException = "
                    + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    public static synchronized List<WECHATBean> get_List_Top_Without_Report(Context context) {
        List<WECHATBean> arrayList = new ArrayList<>();
        Cursor c = null;
        try {
            c = context.getContentResolver()
                    .query(Provider.WECHATColumns.CONTENT_URI,
                            new String[]{Provider.WECHATColumns._ID,
                                    Provider.WECHATColumns.codeUrl,
                                    Provider.WECHATColumns.money,
                                    Provider.WECHATColumns.reason,
                                    Provider.WECHATColumns.treated},
                            Provider.WECHATColumns.treated + "=? and "
                                    + Provider.WECHATColumns.codeUrl + "!=?",
                            new String[]{"1" + "", "" + ""}, "_id DESC limit " +
                                    String.valueOf(PayContext.OFFICE_NUM));

            while (c != null && c.moveToNext()) {
                WECHATBean bean = new WECHATBean();
                bean.id = c.getInt(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns._ID));
                bean.codeUrl = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.codeUrl));
                bean.money = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.money));
                bean.reason = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.reason));
                bean.treated = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.treated));
                arrayList.add(bean);
            }
            return arrayList;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            ExceptionUitl.getInstance().saveExceptionFile("ProviderHelper IllegalArgumentException = "
                    + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    public static synchronized List<WECHATBean> get_List_Without_Report(Context context) {
        List<WECHATBean> arrayList = new ArrayList<>();
        Cursor c = null;
        try {
            c = context.getContentResolver()
                    .query(Provider.WECHATColumns.CONTENT_URI,
                            new String[]{Provider.WECHATColumns._ID,
                                    Provider.WECHATColumns.codeUrl,
                                    Provider.WECHATColumns.money,
                                    Provider.WECHATColumns.reason,
                                    Provider.WECHATColumns.treated},
                            Provider.WECHATColumns.treated + "=? and "
                                    + Provider.WECHATColumns.codeUrl + "!=?",
                            new String[]{"1" + "", "" + ""}, null);

            while (c != null && c.moveToNext()) {
                WECHATBean bean = new WECHATBean();
                bean.id = c.getInt(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns._ID));
                bean.codeUrl = c.getString(c
                        .getColumnIndexOrThrow(codeUrl));
                bean.money = c.getString(c
                        .getColumnIndexOrThrow(money));
                bean.reason = c.getString(c
                        .getColumnIndexOrThrow(reason));
                bean.treated = c.getString(c
                        .getColumnIndexOrThrow(treated));
                arrayList.add(bean);
            }
            return arrayList;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            ExceptionUitl.getInstance().saveExceptionFile("ProviderHelper IllegalArgumentException = "
                    + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    /**
     * @param id      table id
     * @param codeUrl qrCode url
     * @param money   order money
     * @param reason  marked message
     * @param treated treated status
     */
    private static void update(int id, String codeUrl, String money, String reason, String treated,
                               Context context) {
        ContentValues values = new ContentValues();
        Uri updateIdUri = ContentUris.withAppendedId(
                Provider.WECHATColumns.CONTENT_URI, id);
        values.put(Provider.WECHATColumns.codeUrl, codeUrl);
        values.put(Provider.WECHATColumns.money, money);
        values.put(Provider.WECHATColumns.reason, reason);
        values.put(Provider.WECHATColumns.treated, treated);
        context.getContentResolver().update(updateIdUri, values, null, null);
    }

    /**
     * upload result provider data update db
     * update treated
     */
    public static void update(int id, String treated, Context context) {
        ContentValues values = new ContentValues();
        Uri updateIdUri = ContentUris.withAppendedId(
                Provider.WECHATColumns.CONTENT_URI, id);
        values.put(Provider.WECHATColumns.treated, treated);
        context.getContentResolver().update(updateIdUri, values, null, null);
    }

    /**
     * xposed get qrCodeUrl  update data
     *
     * @param reason  message or order num
     * @param codeUrl codeUrl
     * @param context context
     */
    public static void updateCodeUrlWithReason(String reason, String codeUrl, Context context) {
        if (reason == null || "".equals(reason))
            return;
        Cursor c = null;
        try {
            c = context.getContentResolver().query(
                    Provider.WECHATColumns.CONTENT_URI,
                    new String[]{Provider.WECHATColumns._ID,
                            Provider.WECHATColumns.codeUrl,
                            Provider.WECHATColumns.money,
                            Provider.WECHATColumns.reason,
                            Provider.WECHATColumns.treated},
                    Provider.WECHATColumns.reason + "=? and "
                            + Provider.WECHATColumns.money + "!=?",
                    new String[]{reason + "", "0" + ""}, null);
            LogUtil.log("updateCodeUrlWithReason c.size = " + c.getCount());
            WECHATBean bean = new WECHATBean();
            while (c != null && c.moveToNext()) {
                bean.id = c.getInt(c.getColumnIndexOrThrow(Provider.WECHATColumns._ID));
                bean.codeUrl = codeUrl;
                bean.money = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.money));
                bean.reason = c.getString(c
                        .getColumnIndexOrThrow(Provider.WECHATColumns.reason));
                bean.treated = "1";
                update(bean.id, bean.codeUrl, bean.money, bean.reason, bean.treated, context);
                LogUtil.log("updateCodeUrlWithReason bean = " + bean.toString());
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            ExceptionUitl.getInstance().saveExceptionFile("ProviderHelper IllegalArgumentException = "
                    + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    /**
     * insert new data when  receive server message
     */
    public static void insertWX(String codeUrl, String money, String reason,
                                String treated, Context context) {
        try {
            ContentValues values = new ContentValues();
            values.put(Provider.WECHATColumns.codeUrl, codeUrl);
            values.put(Provider.WECHATColumns.money, money);
            values.put(Provider.WECHATColumns.reason, reason);
            values.put(Provider.WECHATColumns.treated, treated);
            context.getContentResolver().insert(Provider.WECHATColumns.CONTENT_URI,
                    values);
        } catch (Exception e) {
            e.printStackTrace();
            MainActivity.instance.log("insertWX error");
        }
    }

    /**
     * insert new data format model
     */
    public static void insertWX(WECHATBean WECHATBean, Context context) {
        try {
            ContentValues values = new ContentValues();
            values.put(Provider.WECHATColumns.codeUrl, WECHATBean.getCodeUrl());
            values.put(Provider.WECHATColumns.money, WECHATBean.getMoney());
            values.put(Provider.WECHATColumns.reason, WECHATBean.getReason());
            values.put(Provider.WECHATColumns.treated, WECHATBean.getTreated());
            context.getContentResolver().insert(Provider.WECHATColumns.CONTENT_URI,
                    values);
        } catch (Exception e) {
            e.printStackTrace();
            MainActivity.instance.log("insertWX error");
        }
    }

    public static void deleteWechat(Context context) {
        try {
            context.getContentResolver().delete(
                    Provider.WECHATColumns.CONTENT_URI, null, null);
            MainActivity.instance.log("cleanWXData success");
        } catch (Exception e) {
            e.printStackTrace();
            MainActivity.instance.log("cleanWXData fail");
        }
    }
}
